package com.triangle.cashier.adapter;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.triangle.cashier.R;
import com.triangle.cashier.bean.ProductCategoryItem;
import com.triangle.cashier.bean.SelectedGoodsItem;

import java.util.List;

/**
 * 商品类别适配器
 */
public class ProductCategoryAdapter extends BaseAdapter{
    private LayoutInflater mInflater;//得到一个LayoutInfalter对象用来导入布局
    private Context context;
    private List<ProductCategoryItem> list;
    private OnChangeOnListener onChangeOnListener;

    public ProductCategoryAdapter(Context context, List<ProductCategoryItem> list, OnChangeOnListener listener) {
        this.context = context;
        this.list = list;
        this.mInflater = LayoutInflater.from(context);
        this.onChangeOnListener = onChangeOnListener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.product_category_item, null);
            holder = new ViewHolder();
            holder.categoryIcon = convertView.findViewById(R.id.category_icon);
            holder.categoryText = convertView.findViewById(R.id.category_text);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ProductCategoryItem item = list.get(position);
        holder.categoryIcon.setImageResource(item.getIconId());
        holder.categoryText.setText(item.getCategoryText());
        return convertView;
    }


    private void refresh() {
        this.notifyDataSetChanged();
    }


    public interface OnChangeOnListener {
        void refresh();
    }

    /**
     * 存放控件
     */
    private final class ViewHolder {
        ImageView categoryIcon;
        TextView categoryText;
    }


}
